package ie.dcu.spca.view.product;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import ie.dcu.spca.Main;
import ie.dcu.spca.R;
import ie.dcu.spca.data.ProductDataManager;
import ie.dcu.spca.model.bean.Product;
import ie.dcu.spca.view.CRUDDialog;

public class ProductCRUDDialog extends CRUDDialog {

    private static ProductDataManager productManager;

    private Button barcodeScanButton;

    private BarcodeScanResultActivity barcodeScanActivity;

    private ProductDetailForm form;

    @Override
    protected void addOkButtonClickOpt(View v) {
        productManager.create(form.getName(), form.getBarcode(), form
                .getPrice(), form.getCategoryId(), form.getUnitId());
    }

    @Override
    protected void bindWidgetOnCreate() {
        form = new ProductDetailForm(this);
        form.bindWidgetOnCreate();
        barcodeScanButton = (Button) this.findViewById(R.id.barcodeScanButton);
        barcodeScanButton
                .setOnClickListener(new BarcodeScanButtonOnClickListener(this));
        barcodeScanActivity = new BarcodeScanResultActivity();
        barcodeScanActivity.setScanResultTextEdit(form.getEtBarcode());
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        barcodeScanActivity.onActivityResult(requestCode, resultCode, intent);
    }

    @Override
    protected void deleteBeanOkButtonClickOpt(DialogInterface dialog) {
        productManager.delete(id);
    }

    @Override
    protected String getDeleteDataConfirmTip() {
        return this.getString(R.string.deleteProductConfirmTip)
                + form.getName();
    }

    @Override
    protected int getContentView() {
        return R.layout.productcruddia;
    }

    @Override
    protected boolean initializeValuesFromWidget() {
        form.initializeValuesFromWidget();
        return true;
    }

    @Override
    protected void setValueFromIntentExtrasOnCreate() {
        productManager = Main.dataManager.productDataManager();
    }

    @Override
    protected void updateBeanWidgetInitialize() {
        Product product = productManager.get(id);
        form.updateBeanWidgetInitialize(product);
    }

    @Override
    protected void updateOkButtonClickOpt(View v) {
        productManager.update(id, form.getName(), form.getBarcode(), form
                .getPrice(), form.getCategoryId(), form.getUnitId());
    }

}
